home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16032 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  86 lines

  1. Path: news.imag.fr!usenet
  2. From: Agnes Poyet <Agnes.Poyet@imag.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Q : pointer to member function
  5. Date: 9 Apr 1996 09:12:17 GMT
  6. Organization: IMAG, Grenoble, France
  7. Message-ID: <4kd9lh$q3q@imag.imag.fr>
  8. NNTP-Posting-Host: curie.imag.fr
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (X11; I; OSF1 V3.2 alpha)
  13. X-URL: news:comp.lang.c++
  14.  
  15. I would like to have a class Minimization whose one member is a
  16. pointer to a function (that is the function to be minimized) :
  17.  
  18.     class Minim
  19.       {
  20.       public :
  21.     //...
  22.         double (* f_to_be_minimized)(double param);
  23.         double minimize(double& param)
  24.                   // minimize the *f_to_be_minimized member function
  25.           {
  26.             //...
  27.             (*f_to_be_minimized)(current_param);  // (2)
  28.             //...
  29.           }
  30.     //...
  31.       }
  32.  
  33. I have an other class which implements a problem to solve and
  34. contains a cost function which has to be minimized :
  35.  
  36.     class Problem
  37.       {
  38.       public :
  39.     //...
  40.         double f_cost(double param);
  41.     //...
  42.       }
  43.  
  44. Then I would like to solve my problem in the following way :
  45.  
  46.     main()
  47.       {
  48.         Minim m;
  49.         Problem my_pb;
  50.         double final_param = INIT_PARAM;
  51.  
  52.         //...
  53.         m.f_to_be_minimized = &(my_pb.f_cost);  // (1)
  54.         m.minimize(final_param);
  55.         //...
  56.       }
  57.  
  58. Unfortunately, the compiler complains :
  59.     assignment to `double (*)(double)' from `double (Problem::*)(double)'
  60. for the line (1), which is quite understandable.
  61.  
  62.  
  63. So I intend to define the Minim class in the less general following way :
  64.  
  65.     class Minim
  66.       {
  67.       public :
  68.     //...
  69.         double (Problem::* f_to_be_minimized)(double param);
  70.         double minimize(double& param);
  71.           // minimize the *f_to_be_minimized member function
  72.     //...
  73.       }
  74.  
  75. The compiler complains again :
  76.     invalid use of `unary *' on pointer to member function
  77. for the line (2).
  78.  
  79.  
  80.     Can someone explain me this error message and give me an idea to
  81. solve my problem ? Thank you in advance.
  82.  
  83.  
  84.             Agnes
  85.  
  86.